package com.itheima.dao;

import com.itheima.domain.User;
import com.itheima.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements Dao{
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet res;

    @Override
    public User login(String username, String password) {
        User u = null;
        try {
            conn = JdbcUtil.getConn();
            String sql = "select * from user where username =? and pwd = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,username);
            pst.setString(2,password);
            res = pst.executeQuery();

            if(res.next()){
                u = new User();
                u.setId(res.getInt("id"));
                u.setUsername(username);
                u.setPassword(password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, pst, res);
        }
        return u;
    }
}
